package com.abu.classes.controller;


import com.abu.classes.pojo.Classes;
import com.abu.classes.service.ClassesService;
import com.abu.classes.utils.PageUtils;
import com.abu.common.Resp;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 班级表 前端控制器
 * </p>
 *
 * @author 阿布
 * @since 2021-05-07
 */
@RestController
@RequestMapping("/class")
public class ClassesController {

    @Autowired
    private ClassesService classesService;

    @GetMapping("noAuthorize/condition")
    public Resp queryByCondition(@RequestParam(value = "page", required = false, defaultValue = "1") Long page,
                                 @RequestParam(value = "limit", required = false, defaultValue = "10") Long limit,
                                 @RequestParam(value = "key", required = false) String key) {
        PageUtils pageResult = this.classesService.queryByConditon(page, limit, key);
        return Resp.ok().data("pageResult", pageResult);
    }

    @GetMapping("noAuthorize/classId/{id}")
    public Resp queryClassesById(@PathVariable("id") Long classId) {
        Classes classes = this.classesService.getById(classId);
        if (classes != null) {
            return Resp.ok().data("classes", classes);
        }
        return Resp.error().msg("查询为空");
    }

    @GetMapping("noAuthorize/classNameById/{id}")
    public Resp queryClassNameByClassId(@PathVariable("id") Long classId) {
        Classes classes = this.classesService.getOne(new QueryWrapper<Classes>().eq("class_id", classId).select("class_name"));
        if (classes != null) {
            return Resp.ok().data("classes", classes);
        }
        return Resp.error().msg("查询为空");
    }

    @GetMapping("noAuthorize/all/class")
    public Resp queryAllClasses() {
        List<Classes> classesList = this.classesService.queryAllClasses();
        if (CollectionUtils.isEmpty(classesList)) {
            return Resp.error().msg("数据为空");
        }
        return Resp.ok().data("classesList", classesList);
    }

    @PostMapping("save")
    public Resp saveClasses(@RequestBody Classes classes) {
        Boolean flag = this.classesService.saveClasses(classes);
        if (flag) {
            return Resp.ok().msg("添加成功");
        }
        return Resp.error().msg("添加失败");
    }

    @PutMapping("entity")
    public Resp updateByClassId(@RequestBody Classes classes) {
        boolean flag = this.classesService.updateById(classes);
        if (flag) {
            return Resp.ok().msg("更新成功");
        }
        return Resp.error().msg("更新失败");
    }

    @DeleteMapping("id/{id}")
    public Resp deleteByIds(@PathVariable("id") Long classId) {
        boolean flag = this.classesService.removeById(classId);
        if (flag) {
            return Resp.ok().msg("删除成功");
        }
        return Resp.error().msg("删除失败");
    }

    @DeleteMapping("ids")
    public Resp deleteBatchByIdList(List<Long> ids) {
        boolean flag = this.classesService.removeByIds(ids);
        if (flag) {
            return Resp.ok().msg("批量删除成功");
        }
        return Resp.error().msg("批量删除失败");
    }


}

